package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    /**
     * 添加商机
     */
    @PostMapping
    public Result add(@RequestBody  Business business){
        log.info("添加商机{}",business);
        business.setStatus(2);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        // 设置当前用户ID作为商机归属人ID
        business.setUserId(com.qk.utils.CurrentUserHoler.getCurrentUser());
        businessService.save(business);
         return Result.success();
    }
    /**
     * 8. 列表查询
     */
    @GetMapping()
    public Result list(BusinessDto businessDto){
        log.info("查询公海池{}",businessDto);
        PageResult<Business> pageResult = businessService.querylist(businessDto);
        return Result.success(pageResult);

    }
    /**
     * 查询商机，根据ID查询商机
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询商机{}", id);
        Business business = businessService.selectById(id);
        return Result.success(business);


    }
    /**
     * 分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机{}", businessId);
        Business business = new Business();
        business.setId(businessId);
        business.setStatus(2);
        business.setUserId(userId);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    /**
     * 更新商机
     */
    @PutMapping
    public Result update(@RequestBody Business business) {
        log.info("修改商机{}", business);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateBusiness(business);
        return Result.success();
    }
    /**
     * 踢回公海
     */
    @PutMapping("/back/{id}")
    public Result backSea(@PathVariable Integer id){
           Business business = new Business();
           business.setId(id);
           business.setStatus(4);
           business.setUpdateTime(LocalDateTime.now());
           businessService.updateById(business);
           return Result.success();

    }
    /**
     * 转客户
     */
   @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        log.info("转客户{}",id);
        businessService.toCustomer(id);
        return Result.success();
    }
    /**
     * 公海池查询
     */
    @GetMapping("/pool")
    public Result pool(BusinessDto businessDto) {
        log.info("查询公海池{}", businessDto);
        PageResult<Business> pageResult = businessService.querylistPool(businessDto);
        return Result.success(pageResult);
    }

}
